package com.jiukuaidao.zhulin.module.address;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiukuaidao.library.common.dialog.CustomDialog;
import com.jiukuaidao.library.util.ScreenInfo;
import com.jiukuaidao.library.widget.banner.view.ForcedSlidingViewPager;
import com.jiukuaidao.library.widget.wheelview.LoopView;
import com.jiukuaidao.library.widget.wheelview.OnItemSelectedListener;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.entity.ApiCityListInfo;
import com.jiukuaidao.zhulin.module.address.bean.EventAddressBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/15
 * Description :
 */
public class CityListBottomDialog {
    private Context mContext;
    private RelativeLayout rootview;
    private LinearLayout mCancel;
    private TextView mTvCancel;
    private TextView mTvSure;
    private ApiCityListInfo mCityListInfo;
    private EventAddressBean mAddressBean; // 发送消息bean
    private int mIndex, mClickIndex;
    private String mCode;

    /**
     * @param context
     * @param cityListInfo
     * @param code         判断 是否与选择器中的code 一致
     */
    public CityListBottomDialog(Context context, final ApiCityListInfo cityListInfo, String code) {
        this.mContext = context;
        this.mCityListInfo = cityListInfo;
        this.mCode = code;
    }

    public void showDialog() {
        ScreenInfo screenInfo = new ScreenInfo((Activity) mContext);
        final CustomDialog dialog = new CustomDialog(mContext, R.style.customDialogStyle);
        View view = LayoutInflater.from(mContext).inflate(R.layout.wheel_text, null);
        rootview = (RelativeLayout) view.findViewById(R.id.rootview);
        mCancel = (LinearLayout) view.findViewById(R.id.ll_cancel);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvSure = (TextView) view.findViewById(R.id.tv_sure);
        LoopView loopView = new LoopView(mContext);
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < mCityListInfo.cityList.size(); i++) {
            list.add(mCityListInfo.cityList.get(i).cityName);
        }
        //设置是否循环播放
        //loopView.setNotLoop();
        //滚动监听
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mIndex = index;
            }
        });
        //设置原始数据
        loopView.setItems(list);
        //设置初始位置
        loopView.setInitPosition(0);
        //设置字体大小
        loopView.setTextSize(20);
        loopView.setViewPadding(screenInfo.getWidth() / 3, 0, screenInfo.getWidth() / 3, 0);
        rootview.addView(loopView);
        dialog.setContentView(view);
        //dialog.setOnDismissListener(this);
        dialog.getWindow().setWindowAnimations(R.style.commonAnimDialogStyle);
        dialog.show();
        dialog.getWindow().setLayout(screenInfo.getWidth(), ForcedSlidingViewPager.LayoutParams.MATCH_PARENT);
        view.setFocusableInTouchMode(true);
        mCancel.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    return true;
                }
                return false;
            }
        });
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAddressBean = new EventAddressBean();
                mAddressBean.cityCode = mCityListInfo.cityList.get(mIndex).cityCode;
                mAddressBean.cityName = mCityListInfo.cityList.get(mIndex).cityName;
                if (!mCode.equals(mAddressBean.cityCode)) {
                    EventBus.getDefault().post(mAddressBean);
                }
                dialog.dismiss();
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}

